from django.db import models

# Create your models here.

#账号
class User(models.Model):
    username = models.CharField(max_length=50,primary_key=True)   #用户名
    password = models.CharField(max_length=50)   #密码
    name = models.CharField(max_length=50)   #姓名
    level = models.CharField(max_length=10,default=0)   #账号等级：1售货员 2领导 3管理员
    status = models.IntegerField(default=1)   # 用户状态 1：启用   0：禁用

    def __str__(self):
        return self.name

# 登录日志
class Log(models.Model):
    userId = models.CharField(max_length=50)  # 登录的用户名
    time = models.CharField(max_length=50)  # 登录的时间
    ip = models.CharField(max_length=50)  # 登录的ip地址
    level = models.IntegerField(default=0)  # 用户的等级

    def __str__(self):
        return self.userId+" "+str(self.level)+" "+self.ip+" "+self.time


#商品
class Goods(models.Model):
    name = models.CharField(max_length=50)   #商品名称
    price = models.FloatField()    #商品价格
    type = models.ForeignKey('GoodsType',on_delete=models.CASCADE)   #商品类别
    preferential = models.BooleanField(default=False)   #是否有打折优惠
    preferentialPrice = models.FloatField()    #优惠后价格
    sum = models.FloatField(default=0)    #商品库存
    soldSum = models.FloatField(default=0)  # 已售数量
    unitId = models.ForeignKey('Unit',on_delete=models.CASCADE)    #单位id
    picture = models.ForeignKey('Pic',on_delete=models.CASCADE)    #商品图片
    isShelves = models.BooleanField(default=True)  # 是否上架

    def __str__(self):
        return self.name

#商品单位
class Unit(models.Model):
    name = models.CharField(max_length=50)   #单位名称

    def __str__(self):
        return self.name

#商品类别
class GoodsType(models.Model):
    name = models.CharField(max_length=50)   #商品类别名称

    def __str__(self):
        return self.name

#交易记录
class TransactionRecords(models.Model):
    ids = models.CharField(max_length=50)    #交易单号
    Date = models.DateTimeField()    #交易时间
    goodsId = models.ForeignKey('Goods',on_delete=models.CASCADE)     #交易商品id
    sum = models.FloatField()      #交易数量
    Price = models.FloatField()   #商品单价
    preferential = models.BooleanField(default=False)  # 是否有打折优惠
    sumPreferentialPrice = models.FloatField()  # 优惠后价格
    userId = models.ForeignKey('User',on_delete=models.CASCADE)   #交易处理人id

    def __str__(self):
        return self.ids

#总菜单列表
class AllMenu(models.Model):
    pid = models.CharField(max_length=50)     #菜单的父级菜单  如果为0则为一级菜单
    name = models.CharField(max_length=50)    #菜单名称
    path = models.CharField(max_length=50)    #菜单路径
    gmt_create = models.DateTimeField()       #创建时间
    gmt_modify = models.DateTimeField()       #修改时间

    def __str__(self):
        return self.name

#权限等级
class AllLevel(models.Model):
    name = models.CharField(max_length=50)    #等级名称
    gmt_create = models.DateTimeField()  # 创建时间
    gmt_modify = models.DateTimeField()  # 修改时间

    def __str__(self):
        return self.name

#角色菜单
class MenuLevel(models.Model):
    level_id = models.IntegerField()   #等级id
    menu_id = models.IntegerField()   #菜单id
    pid = models.CharField(max_length=50)  # 菜单的父级菜单  如果为0则为一级菜单
    name = models.CharField(max_length=50)  # 菜单名称
    path = models.CharField(max_length=50)  # 菜单路径
    gmt_create = models.DateTimeField()  # 创建时间
    gmt_modify = models.DateTimeField()  # 修改时间

    def __str__(self):
        return self.name

"""角色菜单"""
class LevelMenu(models.Model):
    level = models.ForeignKey('AllLevel',on_delete=models.CASCADE)
    menu = models.ForeignKey('AllMenu',on_delete=models.CASCADE)
    gmt_create = models.DateTimeField(null=True)  # 创建时间
    gmt_modify = models.DateTimeField(null=True)  # 修改时间

    def __str__(self):
        return self.level.name + '-' + self.menu.name

"""商品图片"""
class Pic(models.Model):
    name = models.CharField(max_length=50)
    file = models.ImageField(upload_to='GoodsImage')  # 商品图片

    def __str__(self):
        return self.name






